package com.aliya.glide

import android.graphics.drawable.Drawable
import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.aliya.glide.databinding.ActivityLifecycleBinding
import com.aliya.glide.databinding.FragmentLifecyclesBinding
import com.aliya.glide.ktx.viewBindings
import com.bumptech.glide.Glide
import com.bumptech.glide.request.target.DrawableImageViewTarget
import com.bumptech.glide.request.transition.Transition


/**
 * Fragment
 */
class LifecyclesFragment : Fragment() {
    companion object {
        const val TAG = "LifecyclesFragment"
    }


    private val viewBindings by viewBindings(FragmentLifecyclesBinding::bind)


    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        return inflater.inflate(R.layout.fragment_lifecycles, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        viewBindings.loadByView.setOnClickListener {
            viewBindings.ivImage.run {
                Glide.with(this)
                    .load("https://up.enterdesk.com/edpic_source/53/0a/da/530adad966630fce548cd408237ff200.jpg")
                    .placeholder(R.drawable.image_placeholder)
                    .error(R.drawable.image_error)
                    .into(object : DrawableImageViewTarget(this) {

                        override fun onLoadCleared(placeholder: Drawable?) {
                            super.onLoadCleared(placeholder)
                            Log.e(TAG, "Target#onLoadCleared: $placeholder")
                        }

                        override fun onLoadFailed(errorDrawable: Drawable?) {
                            super.onLoadFailed(errorDrawable)
                            Log.e(TAG, "Target#onLoadFailed: $errorDrawable")
                        }

                        override fun onLoadStarted(placeholder: Drawable?) {
                            super.onLoadStarted(placeholder)
                            Log.e(TAG, "Target#onLoadStarted: $placeholder")
                        }

                        override fun onResourceReady(
                            resource: Drawable,
                            transition: Transition<in Drawable>?
                        ) {
                            super.onResourceReady(resource, transition)
                            Log.e(TAG, "Target#onResourceReady: $resource")
                        }

                        override fun onStart() {
                            super.onStart()
                            Log.e(TAG, "BaseTarget#onStart: ")
                        }

                        override fun onStop() {
                            super.onStop()
                            Log.e(TAG, "BaseTarget#onStop: ")
                        }

                        override fun onDestroy() {
                            super.onDestroy()
                            Log.e(TAG, "BaseTarget#onDestroy: ")
                        }

                    })

            }
        }

        viewBindings.loadPause.setOnClickListener {
            viewBindings.ivImage.run {
//                Glide.with(this).pauseRequests() // 取消 Glide.with() 绑定组件管理的进行中的 Request;
                Glide.with(this).pauseRequestsRecursive() // 取消 Glide.with() 绑定组件及依附子组件管理的进行中的 Request;

            }
        }

        viewBindings.loadClear.setOnClickListener {
            viewBindings.ivImage.run {
                Glide.with(this).clear(this) // 取消加载，释放资源

            }
        }
    }

}